import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

public class ForgetJFrame extends JFrame implements ActionListener {
    ArrayList<Account> list=null;
    LoginJFrame loginJFrame=null;
    JButton ensureB=new JButton("确认");
    JButton returnB=new JButton("返回");
    JTextField userNamefield=new JTextField();
    JPasswordField idfied=new JPasswordField();
    JPasswordField passwordField=new JPasswordField();
    JPasswordField okpasswordField=new JPasswordField();
    JDialog userNamewarn=new JDialog();
    JDialog passwordwarn=new JDialog();
    JDialog idwarn=new JDialog();
    public ForgetJFrame(ArrayList<Account> list, LoginJFrame loginJFrame) {
        this.list=list;
        this.loginJFrame=loginJFrame;
        JFrameInit();
        forgetInit();
    }
    private void JFrameInit() {
        this.setTitle("忘记密码");
        this.setSize(400,400);
        this.setLocationRelativeTo(null);
    }

    public void forgetInit() {
        this.getContentPane().removeAll();
        this.setLayout(null);

        JButton userNameB=new JButton("用户名");
        JButton idB=new JButton("身份证");
        JButton passwordB=new JButton("密码");
        JButton okpasswordB=new JButton("确认密码");

        userNameB.setBounds(50,30,90,30);
        idB.setBounds(50,110,90,30);
        passwordB.setBounds(50,190,90,30);
        okpasswordB.setBounds(50,270,90,30);
        ensureB.setBounds(90,330,70,30);
        returnB.setBounds(230,330,70,30);

        this.getContentPane().add(idB);
        this.getContentPane().add(userNameB);
        this.getContentPane().add(passwordB);
        this.getContentPane().add(okpasswordB);
        this.getContentPane().add(ensureB);
        this.getContentPane().add(returnB);

        ensureB.addActionListener(this);
        returnB.addActionListener(this);

        userNamefield.setBounds(160,30,200,30);
        idfied.setBounds(160,110,200,30);
        passwordField.setBounds(160,190,200,30);
        okpasswordField.setBounds(160,270,200,30);
        this.getContentPane().add(userNamefield);
        this.getContentPane().add(idfied);
        this.getContentPane().add(passwordField);
        this.getContentPane().add(okpasswordField);

        this.repaint();
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if(actionEvent.getSource()==returnB){
            forgetInit();
            this.setVisible(false);
            loginJFrame.setVisible(true);
        }else {
            if(actionEvent.getSource()==ensureB){
                String userName=userNamefield.getText();
                String id= new String(idfied.getPassword());
                String password=new String(passwordField.getPassword());
                String okpassword=new String(okpasswordField.getPassword());
                int index=userNameCotain(list,userName);
                if(index>=0){
                    if(list.get(index).getId().equals(id)){
                        if(password.matches(".{6,9}")&&password.equals(okpassword)){
                            list.get(index).setPassword(password);
                            forgetInit();
                            this.setVisible(false);
                            userNamefield.setVisible(false);
                            loginJFrame.setVisible(true);
                        }
                        else {
                            passwordwarn.setLocationRelativeTo(null);
                            passwordwarn.setSize(300,100);
                            passwordwarn.setVisible(true);
                            JLabel tip1=new JLabel("密码格式有误,或密码不一致      --按任意键继续");
                            passwordwarn.getContentPane().add(tip1);
                            passwordwarn.addKeyListener(new KeyListener() {
                                @Override
                                public void keyTyped(KeyEvent keyEvent) {

                                }

                                @Override
                                public void keyPressed(KeyEvent keyEvent) {

                                }

                                @Override
                                public void keyReleased(KeyEvent keyEvent) {
                                    forgetInit();
                                    passwordwarn.setVisible(false);
                                }
                            });
                        }
                    }else {
                        JLabel tip2=new JLabel("身份证有误      --按任意键继续");
                        idwarn.setLocationRelativeTo(null);
                        idwarn.setSize(300,100);
                        idwarn.setVisible(true);
                        idwarn.getContentPane().add(tip2);
                        idwarn.addKeyListener(new KeyListener() {
                            @Override
                            public void keyTyped(KeyEvent keyEvent) {

                            }

                            @Override
                            public void keyPressed(KeyEvent keyEvent) {

                            }

                            @Override
                            public void keyReleased(KeyEvent keyEvent) {
                                forgetInit();
                                idwarn.setVisible(false);
                            }
                        });
                    }
                }else {
                    JLabel tip3=new JLabel("用户名为空或用户名不存在    --按任意键继续");
                    userNamewarn.setLocationRelativeTo(null);
                    userNamewarn.setSize(300,100);
                    userNamewarn.setVisible(true);
                    userNamewarn.getContentPane().add(tip3);
                    userNamewarn.addKeyListener(new KeyListener() {
                        @Override
                        public void keyTyped(KeyEvent keyEvent) {

                        }

                        @Override
                        public void keyPressed(KeyEvent keyEvent) {

                        }

                        @Override
                        public void keyReleased(KeyEvent keyEvent) {
                            forgetInit();
                            userNamewarn.setVisible(false);
                        }
                    });
                }
            }
        }
    }


    private int userNameCotain(ArrayList<Account> list, String userName) {
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUserName().equals(userName)){
                return i;
            }
        }
        return -1;
    }
}
